//
//  ICMAppearanceCheckView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMAppearanceCheckView.h"
#import "ICMCheckImageCell.h"
#import "ICMAddBelongsView.h"
#import "ICMShowImageView.h"
#import "ICMPictureModel.h"
#import "ICMCheckAppearanceDownModel.h"

@interface ICMAppearanceCheckView()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,ICMCheckImageCellDelegate,ICMAddBelongsViewDelegate>

@property (nonatomic, strong) TPKeyboardAvoidingScrollView *wholeScrollView;

@property (nonatomic, strong) UIScrollView *appearanceScrollView;
@property (nonatomic, strong) UIImageView *appearanceImageView;
@property (nonatomic, strong) UICollectionView *appearanceCollectionView;

@property (nonatomic, strong) UIScrollView *innerScrollView;
@property (nonatomic, strong) UIImageView *innerImageView;
@property (nonatomic, strong) UICollectionView *innerCollectionView;

@property (nonatomic, strong) NSMutableArray *appearanceBreakdownButtons;
@property (nonatomic, strong) NSMutableArray *appearanceBreakdownLabels;
@property (nonatomic, strong) NSMutableArray *innerBreakdownButtons;
@property (nonatomic, strong) NSMutableArray *innerBreakdownLabels;

@property (nonatomic, assign) NSInteger appearanceSelectedIndex;
@property (nonatomic, assign) NSInteger innerSelectedIndex;

@property (nonatomic, strong) NSArray *appearanceBreakdownImages;
@property (nonatomic, strong) NSArray *innerBreakdownImages;

@property (nonatomic, strong) NSMutableArray *appearanceBreakdownImageViews;
@property (nonatomic, strong) NSMutableArray *innerBreakdownImageViews;

@property (nonatomic,assign) BOOL isFromPreview;
@end

@implementation ICMAppearanceCheckView
#pragma mark    初始化
- (instancetype)initAppearanceCheckViewWithFrame:(CGRect)frame andIsfromPreview:(BOOL)isFromPreview{
    if (self = [super initWithFrame:frame]) {
        [self.wholeScrollView contentSizeToFit];
        self.isFromPreview = isFromPreview;
        self.appearanceImages =  [NSMutableArray array];
        self.appearanceUrls = [NSMutableArray array];
        self.innerUrls = [NSMutableArray array];
        self.innerImages = [NSMutableArray array];
        self.appearanceImageView.image = [UIImage imageNamed:[ICrEAMTool isVW]?@"appearance_vw":@"appearance_sk"];
        self.innerImageView.image = [UIImage imageNamed:@"inner_icon"];
        NSString *imageName = [NSString stringWithFormat:@"big_empty_%@",[ICrEAMTool isVW]?@"vw":@"sk"];
        if (self.appearanceImage) {
            [self.appearanceImageView sd_setImageWithURL:[NSURL URLWithString:self.appearanceImage] placeholderImage:[UIImage imageNamed:imageName]];
        }
        if (self.innerImage) {
            [self.innerImageView sd_setImageWithURL:[NSURL URLWithString:self.innerImage] placeholderImage:[UIImage imageNamed:imageName]];
        }
        //
        if(self.isFromPreview == YES){
            self.appearanceImageView.userInteractionEnabled = NO;
            self.innerImageView.userInteractionEnabled = NO;
        }
        UIView *appearanceHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2, 44)];
        for (UIButton *button in self.appearanceBreakdownButtons) {
            [appearanceHeadView addSubview:button];
        }
        UIButton *cancelAppearanceButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-80, 5, 37, 37)];
        [cancelAppearanceButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Cancel] selectedBackImage:[UIImage imageNamed:GetCar_Cancel] actionSEL:@selector(cancelAddAppearanceBreakdownImageButtonAction:) target:self];
        [appearanceHeadView addSubview:cancelAppearanceButton];
        
        if(self.isFromPreview == YES){
            cancelAppearanceButton.hidden = YES;
        }
        [self.wholeScrollView addSubview:appearanceHeadView];
        //
        UIView *innerHeadView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/2, 0, ScreenWidth/2, 44)];
        for (UIButton *button in self.innerBreakdownButtons) {
            [innerHeadView addSubview:button];
        }
        UIButton *cancelInnerButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-80, 5, 37, 37)];
        [cancelInnerButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Cancel] selectedBackImage:[UIImage imageNamed:GetCar_Cancel] actionSEL:@selector(cancelAddInnerBreakdownImageButtonAction:) target:self];
        [innerHeadView addSubview:cancelInnerButton];
        if(self.isFromPreview == YES){
            cancelInnerButton.hidden = YES;
        }
        [self.wholeScrollView addSubview:innerHeadView];
        //
        UIButton *appearanceImageButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 75, 75)];
        [appearanceImageButton upDistanceToAnotherView:self.appearanceCollectionView upDistance:5];
        [appearanceImageButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:@"photo_icon"] selectedBackImage:[UIImage imageNamed:@"photo_icon"] actionSEL:@selector(appearanceImageButtonAction:) target:self];
        //预览的情况不能拍照
        if(self.isFromPreview == YES){
            appearanceImageButton.hidden = YES;
        }
        [self.wholeScrollView addSubview:appearanceImageButton];
        //
        self.innerScrollView.hidden = NO;
        //
        UIButton *innerImageButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2+20, 25, 75, 75)];
        [innerImageButton upDistanceToAnotherView:self.innerCollectionView upDistance:5];
        [innerImageButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:@"photo_icon"] selectedBackImage:[UIImage imageNamed:@"photo_icon"] actionSEL:@selector(innerImageButtonAction:) target:self];
        [self.wholeScrollView addSubview:innerImageButton];
        //预览的情况不能拍照
        if(self.isFromPreview == YES){
            innerImageButton.hidden = YES;
        }
        //
        self.belongingsTextView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(20, 25, ScreenWidth/2-70, 130) title:@"随车物品" textViewHeight:100];
        [self.belongingsTextView upDistanceToAnotherView:self.appearanceCollectionView distance:20];
        [self.wholeScrollView addSubview:self.belongingsTextView];
        //
        self.explainTextView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(ScreenWidth/2+20, 25, ScreenWidth/2-70, 130) title:@"说明" textViewHeight:100];
        [self.explainTextView upDistanceToAnotherView:self.innerCollectionView distance:20];
        self.explainTextView.limitedNum = 100;
        [self.wholeScrollView addSubview:self.explainTextView];
        //
        UIButton *addBelongingsButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        [addBelongingsButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Add] selectedBackImage:[UIImage imageNamed:GetCar_Add] actionSEL:@selector(addBelongingsButtonAction:) target:self];
        [addBelongingsButton upDistanceToAnotherView:self.belongingsTextView upDistance:0];
        [addBelongingsButton rightDistanceToAnotherView:self.belongingsTextView rightDistance:-10];
        [self.wholeScrollView addSubview:addBelongingsButton];
        [ICrEAMTool createAndAddBiggerButtonForButton:addBelongingsButton andSEL:@selector(addBelongingsButtonAction:) target:self addTop:10 bottom:0 left:10 right:10 inView:self.wholeScrollView];
        self.wholeScrollView.contentSize = CGSizeMake(ScreenWidth, CGRectGetMaxY(self.explainTextView.frame)+20);
        
        if(self.isFromPreview == YES){
            addBelongingsButton.hidden = YES;
            self.explainTextView.userInteractionEnabled = NO;
            self.belongingsTextView.userInteractionEnabled = NO;
        }
        
    }
    return self;
}

+ (instancetype)createAppearanceCheckViewWithFrame:(CGRect)frame andIsfromPreview:(BOOL)isFromPreview{
    return [[ICMAppearanceCheckView alloc]initAppearanceCheckViewWithFrame:frame andIsfromPreview:isFromPreview];
}

/***********************buttonAction*****************************/
#pragma mark    添加随车物品按钮事件
- (void)belongingsTextViewDidSelected:(ICMTitleTextView*)titleTextView{
 
    
    NSArray *belongTitles = [ICrEAMTool isVW]?@[@"太阳镜",@"随车工具及备胎",@"酒",@"运动器材",@"独立导航",@"香烟"]:@[@"备胎",@"随车工具"];
    NSMutableArray *allBelongs = [[self.belongingsTextView.contentText componentsSeparatedByString:@"\n"]mutableCopy];
    NSMutableArray *selectedArray = [NSMutableArray array];
    NSMutableArray *inputArray = [NSMutableArray array];
    for (NSString *belong in allBelongs) {
        [belongTitles containsObject:belong]?[selectedArray PACA_AddObject:belong]:[inputArray PACA_AddObject:belong];
    }
    
    ICMAddBelongsView *addBelongsView = [ICMAddBelongsView creatAddBelongsViewWithBelongs:selectedArray.count?selectedArray:nil];
    addBelongsView.delegate = self;
    [Window addSubview:addBelongsView];
}
#pragma mark    外部拍照按钮事件
- (void)appearanceImageButtonAction:(UIButton*)sender{
    [self endEditing:YES];

    if ([self.delegate respondsToSelector:@selector(appearanceCheckViewAppearanceImageButtonAction)]) {
        [self.delegate appearanceCheckViewAppearanceImageButtonAction];
    }
}
#pragma mark    内部拍照按钮事件
- (void)innerImageButtonAction:(UIButton*)sender{
    [self endEditing:YES];

    if ([self.delegate respondsToSelector:@selector(appearanceCheckViewInnerImageButtonAction)]) {
        [self.delegate appearanceCheckViewInnerImageButtonAction];
    }
}
#pragma mark    增加随车物品界面
- (void)addBelongingsButtonAction:(UIButton*)sender{
    [self endEditing:YES];
    [self belongingsTextViewDidSelected:self.belongingsTextView];
}
#pragma mark    外部损伤按钮点击
- (void)appearanceBreakdownButtonAction:(UIButton*)sender{
    self.appearanceSelectedIndex = [self.appearanceBreakdownButtons indexOfObject:sender];
    for (NSInteger i=0; i<self.appearanceBreakdownLabels.count; i++) {
        UILabel *titleLabel = self.appearanceBreakdownLabels[i];
        titleLabel.backgroundColor = i==self.appearanceSelectedIndex?TitleColor:[UIColor clearColor];
    }
}
#pragma mark    内部损伤按钮点击
- (void)innerBreakdownButtonAction:(UIButton*)sender{
    self.innerSelectedIndex = [self.innerBreakdownButtons indexOfObject:sender];
    for (NSInteger i=0; i<self.innerBreakdownLabels.count; i++) {
        UILabel *titleLabel = self.innerBreakdownLabels[i];
        titleLabel.backgroundColor = i==self.innerSelectedIndex?TitleColor:[UIColor clearColor];
    }
}
#pragma mark    取消外部损伤标注
- (void)cancelAddAppearanceBreakdownImageButtonAction:(UIButton*)sender{
    if (!self.appearancePoints.count) {
        return;
    }
    [self.appearancePoints removeLastObject];
    UIImageView *lastAppearanceBreakdownImageView = [self.appearanceBreakdownImageViews lastObject];
    [self.appearanceBreakdownImageViews removeLastObject];
    [lastAppearanceBreakdownImageView removeFromSuperview];
}
#pragma mark    取消内部损伤标注
- (void)cancelAddInnerBreakdownImageButtonAction:(UIButton*)sender{
    if (!self.innerPoints.count) {
        return;
    }
    [self.innerPoints removeLastObject];
    UIImageView *lastInnerBreakdownImageView = [self.innerBreakdownImageViews lastObject];
    [self.innerBreakdownImageViews removeLastObject];
    [lastInnerBreakdownImageView removeFromSuperview];
}
#pragma mark    外部添加损伤标记
- (void)appearanceImageViewTapAction:(UITapGestureRecognizer*)tap{
    CGPoint tapPoint = [tap locationInView:self.appearanceImageView];

    float xRatio = tapPoint.x/CGRectGetWidth(self.appearanceImageView.frame);
    float yRatio = tapPoint.y/CGRectGetHeight(self.appearanceImageView.frame);
    CGPoint pointRatio = CGPointMake(xRatio, yRatio);
    NSDictionary *appearancePointDic = @{NSStringFromCGPoint(pointRatio):[@(self.appearanceSelectedIndex)stringValue]};
    [self.appearancePoints PACA_AddObject:appearancePointDic];
    UIImageView *breakdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    breakdownImageView.center = CGPointMake(pointRatio.x*CGRectGetWidth(self.appearanceImageView.frame), pointRatio.y*CGRectGetHeight(self.appearanceImageView.frame));
    breakdownImageView.image = self.appearanceBreakdownImages[self.appearanceSelectedIndex];
    [self.appearanceImageView addSubview:breakdownImageView];
    [self.appearanceBreakdownImageViews PACA_AddObject:breakdownImageView];
}
#pragma mark    内部添加损伤标记
- (void)innerImageViewTapAction:(UITapGestureRecognizer*)tap{
    CGPoint tapPoint = [tap locationInView:self.innerImageView];
    float xRatio = tapPoint.x/CGRectGetWidth(self.innerImageView.frame);
    float yRatio = tapPoint.y/CGRectGetHeight(self.innerImageView.frame);
    CGPoint pointRatio = CGPointMake(xRatio, yRatio);
    NSDictionary *innerPointDic = @{NSStringFromCGPoint(pointRatio):[@(self.innerSelectedIndex)stringValue]};
    [self.innerPoints PACA_AddObject:innerPointDic];
    UIImageView *breakdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    breakdownImageView.center = CGPointMake(pointRatio.x*CGRectGetWidth(self.innerImageView.frame), pointRatio.y*CGRectGetHeight(self.innerImageView.frame));
    breakdownImageView.image = self.innerBreakdownImages[self.innerSelectedIndex];
    [self.innerImageView addSubview:breakdownImageView];
    [self.innerBreakdownImageViews PACA_AddObject:breakdownImageView];
}

- (void)refreshAppearanceCollectionView{
    dispatch_async(dispatch_get_main_queue(), ^{
           if (!StrIsZero_gdm(self.appearanceImage)) {
               [self.appearanceImageView sd_setImageWithURL:[NSURL URLWithString:self.appearanceImage]];
//               [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:self.appearanceImage] options:SDWebImageDownloaderContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//                   
//               } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
//                   self.appearanceImageView.image = image;
//               }];
        }else{
        self.appearanceImageView.image = [UIImage imageNamed:[ICrEAMTool isVW]?@"appearance_vw":@"appearance_sk"];
        }
        [self.appearanceCollectionView reloadData];
    });

}

- (void)refreshInnerCollectionView{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!StrIsZero_gdm(self.innerImage)) {
            [self.innerImageView sd_setImageWithURL:[NSURL URLWithString:self.innerImage]];
        }else{
        self.innerImageView.image = [UIImage imageNamed:@"inner_icon"];
        }
        [self.innerCollectionView reloadData];
    });
}
#pragma mark    设置外部图片model
- (void)setupAppearancePicModelsWithDic:(NSDictionary *)dic{
    self.picModels = [NSMutableArray array];
    
    ICMPictureModel *picModelInner = [[ICMPictureModel alloc]init];
    picModelInner.DAMAGE_TYPE = ICMEnumIntToString(ICMEnumDamageType36001001);
    picModelInner.PIC_PATCH = ((NSArray*)dic[@"FILE_INNER_BIG"]).firstObject;
    picModelInner.DAMAGE_POSITION = @"";
    [self.picModels PACA_AddObject:picModelInner];
    
    for (NSString *innerPath in self.innerUrls) {
        ICMPictureModel *picModelSmall = [[ICMPictureModel alloc]init];
        picModelSmall.DAMAGE_TYPE = ICMEnumIntToString(ICMEnumDamageType36001002);
        picModelSmall.PIC_PATCH = innerPath;
        picModelSmall.DAMAGE_POSITION = @"";
        [self.picModels PACA_AddObject:picModelSmall];
    }
    
    ICMPictureModel *picModelOuter = [[ICMPictureModel alloc]init];
    picModelOuter.DAMAGE_TYPE = ICMEnumIntToString(ICMEnumDamageType36001003);
    picModelOuter.PIC_PATCH = ((NSArray*)dic[@"FILE_OUTER_BIG"]).firstObject;
    picModelOuter.DAMAGE_POSITION = @"";
    [self.picModels PACA_AddObject:picModelOuter];
    
    for (NSString *outerPath in self.appearanceUrls) {
        ICMPictureModel *picModelSmall = [[ICMPictureModel alloc]init];
        picModelSmall.DAMAGE_TYPE = ICMEnumIntToString(ICMEnumDamageType36001004);
        picModelSmall.PIC_PATCH = outerPath;
        picModelSmall.DAMAGE_POSITION = @"";
        [self.picModels PACA_AddObject:picModelSmall];
    }
}


/****************************属性初始化****************************/
- (TPKeyboardAvoidingScrollView *)wholeScrollView{
    if (!_wholeScrollView) {
        _wholeScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.frame))];
        _wholeScrollView.showsVerticalScrollIndicator = YES;
        _wholeScrollView.showsHorizontalScrollIndicator = NO;
        _wholeScrollView.backgroundColor = BackgroundColor_White;
        [self addSubview:_wholeScrollView];
    }
    return _wholeScrollView;
}

- (UIScrollView *)appearanceScrollView{
    if (!_appearanceScrollView) {
        _appearanceScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, ScreenWidth/2, ScreenWidth/2*456/741)];
        [self primarySetUpScrollView:_appearanceScrollView];
        BorderColor(_appearanceScrollView, 0xE6E6E6);
        BorderWidth(_appearanceScrollView, 1);
        [self.wholeScrollView addSubview:_appearanceScrollView];
    }
    return _appearanceScrollView;
}

- (UIScrollView *)innerScrollView{
    if (!_innerScrollView) {
        _innerScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(ScreenWidth/2, 44, ScreenWidth/2, ScreenWidth/2*456/741)];
        [self primarySetUpScrollView:_innerScrollView];
        BorderColor(_innerScrollView, 0xE6E6E6);
        BorderWidth(_innerScrollView, 1);
        [self.wholeScrollView addSubview:_innerScrollView];
    }
    return _innerScrollView;
}

- (UIImageView *)appearanceImageView{
    if (!_appearanceImageView) {
        _appearanceImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2, ScreenWidth/2*456/741)];
        _appearanceImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(appearanceImageViewTapAction:)];
        tap.numberOfTapsRequired = 1;
        [_appearanceImageView addGestureRecognizer:tap];
        [self.appearanceScrollView addSubview:_appearanceImageView];
    }
    return _appearanceImageView;
}

- (UIImageView *)innerImageView{
    if (!_innerImageView) {
        _innerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2, ScreenWidth/2*456/741)];
        _innerImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(innerImageViewTapAction:)];
        tap.numberOfTapsRequired = 1;
        [_innerImageView addGestureRecognizer:tap];
        [self.innerScrollView addSubview:_innerImageView];
    }
    return _innerImageView;
}

- (void)primarySetUpScrollView:(UIScrollView*)scrollView{
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    scrollView.scrollEnabled = YES;
    scrollView.maximumZoomScale = 2.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.contentSize = scrollView.frame.size;
}

- (UICollectionView *)appearanceCollectionView{
    
    if (!_appearanceCollectionView) {
        CGRect rect;
        
        if(self.isFromPreview == YES){
            rect = CGRectMake(30, 20, ScreenWidth/2-30, 115);
        }else{
            rect = CGRectMake(115, 20, ScreenWidth/2-115, 85);
        }
        _appearanceCollectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:[self getPrimaryCollectionViewFlowLayout]];
        
        [self primarySetUpCollectionView:_appearanceCollectionView];
        [self.wholeScrollView addSubview:_appearanceCollectionView];
    }
    return _appearanceCollectionView;
}

- (UICollectionView *)innerCollectionView{
    if (!_innerCollectionView) {
        CGRect rect;
        if(self.isFromPreview == YES){
            rect = CGRectMake(ScreenWidth/2+30, 20, ScreenWidth/2-30, 115);
        }else{
            rect = CGRectMake(ScreenWidth/2+115, 20, ScreenWidth/2-115, 85);
        }
        
        _innerCollectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:[self getPrimaryCollectionViewFlowLayout]];
        [self primarySetUpCollectionView:_innerCollectionView];
        [self.wholeScrollView addSubview:_innerCollectionView];
    }
    return _innerCollectionView;
}

- (void)primarySetUpCollectionView:(UICollectionView*)collectionView{
    [collectionView upDistanceToAnotherView:self.appearanceScrollView distance:10];
    [collectionView registerClass:[ICMCheckImageCell class] forCellWithReuseIdentifier:@"ICMCheckImageCell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
}

- (UICollectionViewFlowLayout*)getPrimaryCollectionViewFlowLayout{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(85, 85);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 10.0;
    flowLayout.minimumInteritemSpacing = 0.0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    return flowLayout;
}

- (NSArray *)appearanceBreakdownButtons{
    if (!_appearanceBreakdownButtons) {
        _appearanceBreakdownButtons = [NSMutableArray array];
        self.appearanceBreakdownLabels = [NSMutableArray array];
        NSArray *titles = @[@"凹陷",@"划痕",@"石击",@"油漆"];
        float buttonWidth = (ScreenWidth/2-ActualWidth(180))/4;
        for (NSInteger i=0; i<4; i++) {
            UIButton *breakdownButton = [[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(90)+i*buttonWidth, 2, buttonWidth, 40)];
            //
            UIImageView *buttonImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
            buttonImageView.image = self.appearanceBreakdownImages[i];
            [breakdownButton addSubview:buttonImageView];
            //
            UILabel *buttonLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, 10, CGRectGetWidth(breakdownButton.frame)-22, 20)];
            [buttonLabel setUpLabelWithText:titles[i] alignment:NSTextAlignmentLeft textSize:20 textColor:[UIColor blackColor]];
            [breakdownButton addSubview:buttonLabel];
            UILabel *downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, buttonWidth, 1)];
            downLabel.backgroundColor = i==0?TitleColor:[UIColor clearColor];
            [breakdownButton addSubview:downLabel];
            [breakdownButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(appearanceBreakdownButtonAction:) target:self];
            
            [_appearanceBreakdownButtons PACA_AddObject:breakdownButton];
            [self.appearanceBreakdownLabels PACA_AddObject:downLabel];
        }
    }
    return _appearanceBreakdownButtons;
}

- (NSArray *)innerBreakdownButtons{
    if (!_innerBreakdownButtons) {
        _innerBreakdownButtons = [NSMutableArray array];
        self.innerBreakdownLabels = [NSMutableArray array];
        NSArray *titles = @[@"污渍",@"破损",@"色斑",@"变形"];
        float buttonWidth = (ScreenWidth/2-ActualWidth(180))/4;
        for (NSInteger i=0; i<4; i++) {
            UIButton *breakdownButton = [[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(90)+i*buttonWidth, 2, buttonWidth, 40)];
            //
            UIImageView *buttonImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
            buttonImageView.image = self.innerBreakdownImages[i];
            [breakdownButton addSubview:buttonImageView];
            //
            UILabel *buttonLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, 10, CGRectGetWidth(breakdownButton.frame)-22, 20)];
            [buttonLabel setUpLabelWithText:titles[i] alignment:NSTextAlignmentLeft textSize:20 textColor:[UIColor blackColor]];
            [breakdownButton addSubview:buttonLabel];
            
            UILabel *downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, buttonWidth, 1)];
            downLabel.backgroundColor = i==0?TitleColor:[UIColor clearColor];
            [breakdownButton addSubview:downLabel];
            [breakdownButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(innerBreakdownButtonAction:) target:self];
            [_innerBreakdownButtons PACA_AddObject:breakdownButton];
            [self.innerBreakdownLabels PACA_AddObject:downLabel];
        }
    }
    return _innerBreakdownButtons;
}

- (NSArray *)appearanceBreakdownImages{
    if (!_appearanceBreakdownImages) {
        _appearanceBreakdownImages = @[[UIImage imageNamed:@"appearance_1"],[UIImage imageNamed:@"appearance_2"],[UIImage imageNamed:@"appearance_3"],[UIImage imageNamed:@"appearance_4"]];
    }
    return _appearanceBreakdownImages;
}

- (NSArray *)innerBreakdownImages{
    if (!_innerBreakdownImages) {
        _innerBreakdownImages = @[[UIImage imageNamed:@"inner_1"],[UIImage imageNamed:@"inner_2"],[UIImage imageNamed:@"inner_3"],[UIImage imageNamed:@"inner_4"]];
    }
    return _innerBreakdownImages;
}

- (NSMutableArray *)appearancePoints{
    if (!_appearancePoints) {
        _appearancePoints = [NSMutableArray array];
    }
    return _appearancePoints;
}

- (NSMutableArray *)innerPoints{
    if (!_innerPoints) {
        _innerPoints = [NSMutableArray array];
    }
    return _innerPoints;
}

- (NSMutableArray *)appearanceBreakdownImageViews{
    if (!_appearanceBreakdownImageViews) {
        _appearanceBreakdownImageViews = [NSMutableArray array];
    }
    return _appearanceBreakdownImageViews;
}

- (NSMutableArray *)innerBreakdownImageViews{
    if (!_innerBreakdownImageViews) {
        _innerBreakdownImageViews = [NSMutableArray array];
    }
    return _innerBreakdownImageViews;
}


/*****************************scrollViewDelegate*****************************/
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return scrollView == self.appearanceScrollView?self.appearanceImageView:self.innerImageView;
}

#pragma mark    collectionDelegate、collectionDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return collectionView == self.appearanceCollectionView?self.appearanceImages.count+self.appearanceUrls.count :self.innerImages.count+self.innerUrls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ICMCheckImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ICMCheckImageCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ICMCheckImageCell alloc]init];
    }
    cell.delegate = self;
    
    if (collectionView == self.appearanceCollectionView) {
        if (self.appearanceUrls.count == 0 ||indexPath.row>(self.appearanceUrls.count-1)) {
            UIImage *image = self.appearanceImages[indexPath.row-self.appearanceUrls.count];
            [cell setUpCheckImageCellWithImage:image isAppearance:(collectionView == self.appearanceCollectionView) andIndex:indexPath.row];
        }else{
            NSString *urlString = self.appearanceUrls[indexPath.row];
            [cell setUpCheckImageCellWithUrl:urlString isAppearance:(collectionView == self.appearanceCollectionView) andIndex:indexPath.row];
        }
    }else{
        if (self.innerUrls.count == 0 || indexPath.row>(self.innerUrls.count-1)) {
            UIImage *image = self.innerImages[indexPath.row-self.innerUrls.count];
            [cell setUpCheckImageCellWithImage:image isAppearance:(collectionView == self.appearanceCollectionView) andIndex:indexPath.row];
        }else{
            NSString *urlString = self.innerUrls[indexPath.row];
            [cell setUpCheckImageCellWithUrl:urlString isAppearance:(collectionView == self.appearanceCollectionView) andIndex:indexPath.row];
        }
    }
    
    if(self.isFromPreview == YES){
        [cell setDeleteButtonHidden];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *images = [[NSMutableArray alloc] init];
    if(collectionView == self.appearanceCollectionView)
    {
        for(int i= 0;i<self.appearanceUrls.count;i++)
        {
            UIImageView *imageView = [[UIImageView alloc] init];
            NSString *imageName = [NSString stringWithFormat:@"getcar_empty_image_%@",[ICrEAMTool isVW]?@"vw":@"sk"];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.appearanceUrls[i]] placeholderImage:[UIImage imageNamed:imageName]];
            [images addObject:imageView.image];
        }
        for(int j = 0;j<self.appearanceImages.count;j++)
        {
            [images addObject:self.appearanceImages[j]];
        }
    }
    else
    {
        for(int i= 0;i<self.innerUrls.count;i++)
        {
            UIImageView *imageView = [[UIImageView alloc] init];
            NSString *imageName = [NSString stringWithFormat:@"getcar_empty_image_%@",[ICrEAMTool isVW]?@"vw":@"sk"];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.innerUrls[i]] placeholderImage:[UIImage imageNamed:imageName]];
            [images addObject:imageView.image];
        }
        for(int j = 0;j<self.innerImages.count;j++)
        {
            [images addObject:self.innerImages[j]];
        }
    }
    
    [ICMShowImageView createShowImageViewWithImage:images currentIndex:indexPath.row IsPreview:self.isFromPreview];
}

/*****************ICMCheckImageCellDelegate**************/
- (void)checkImageCellDeleteImageButtonAction:(BOOL)isAppearance andIndex:(NSInteger)index{
    if (isAppearance) {
        if (self.appearanceUrls.count == 0 || index>(self.appearanceUrls.count-1)) {
            [self.appearanceImages removeObjectAtIndex:index-self.appearanceUrls.count];
            [self.appearanceCollectionView reloadData];
        }else{
            [self.appearanceUrls removeObjectAtIndex:index];
            [self.appearanceCollectionView reloadData];
        }
    }else{
        if (self.innerUrls.count == 0 || index>(self.innerUrls.count-1)) {
            [self.innerImages removeObjectAtIndex:index-self.innerUrls.count];
            [self.innerCollectionView reloadData];
        }else{
            [self.innerUrls removeObjectAtIndex:index];
            [self.innerCollectionView reloadData];
        }
    }
}

- (void)setupAppearanceCheckViewWithCheckAppearanceTopModel:(ICMCheckAppearanceTopModel*)checkAppearanceTopModel{
    
}
- (void)setupAppearanceCheckViewWithCheckAppearanceDownModel:(ICMCheckAppearanceDownModel*)checkAppearanceDownModel{
    NSString *belongsSelected = [[checkAppearanceDownModel.VHCL_GOODS componentsSeparatedByString:@"@"] componentsJoinedByString:@"\n"];
    NSString *belongsInput = [[checkAppearanceDownModel.VHCL_GOODS_REMARK componentsSeparatedByString:@"@"] componentsJoinedByString:@"\n"];
    self.belongingsTextView.contentText = [NSString stringWithFormat:@"%@%@%@",belongsSelected.length?belongsSelected:@"",belongsSelected.length?@"\n":@"",belongsInput.length?belongsInput:@""];
    self.explainTextView.contentText = checkAppearanceDownModel.OUT_CHECK_DESC;
}

/*******************ICMAddBelongsView*******************/
#pragma mark    添加随车物品
- (void)addBelongsViewDidSelectedBelongs:(NSMutableArray *)belongs{
    NSArray *belongTitles = [ICrEAMTool isVW]?@[@"太阳镜",@"随车工具及备胎",@"酒",@"运动器材",@"独立导航",@"香烟"]:@[@"备胎",@"随车工具"];
    NSMutableArray *allBelongs = [[self.belongingsTextView.contentText componentsSeparatedByString:@"\n"]mutableCopy];
    NSMutableArray *selectedArray = [NSMutableArray array];
    NSMutableArray *inputArray = [NSMutableArray array];
    for (NSString *belong in allBelongs) {
        [belongTitles containsObject:belong]?[selectedArray PACA_AddObject:belong]:[inputArray PACA_AddObject:belong];
    }
    [allBelongs removeObjectsInArray:selectedArray];
    for (NSInteger i=0; i<allBelongs.count; i++) {
        NSString *belong = allBelongs[i];
        if ([belong isEqualToString:@""]) {
            [allBelongs removeObject:belong];
        }
    }
    
    self.belongingsTextView.contentText = [NSString stringWithFormat:@"%@%@",allBelongs.count?([NSString stringWithFormat:@"%@\n",[allBelongs componentsJoinedByString:@"\n"]]):@"",belongs.count?([NSString stringWithFormat:@"%@\n",[belongs componentsJoinedByString:@"\n"]]):@""];
}


- (UIImage *)getImageWithType:(NSInteger)type{
    //0外部 1内部
    if (type == 0) {
        UIGraphicsBeginImageContext(self.appearanceImageView.bounds.size);
        [self.appearanceImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }else{
        UIGraphicsBeginImageContext(self.innerImageView.bounds.size);
        [self.innerImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

@end






